//
//  WatermarkSelectView.swift
//  WatermarkCamera
//
//  Created by 赵新 on 5/7/2024.
//

import UIKit
import AxcBedrock

// MARK: - [WatermarkSelectView]

class WatermarkSelectView: SelectView {
    override func createUI() {
        super.createUI()
        title = "选择水印"
        titleView.isShowLine = true
        titleView.leftBtn.setImage("jinzhi".axc.uiImage, for: .normal)
        titleView.rightBtn.setImage("close".axc.uiImage, for: .normal)

        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }

    let inset: UIEdgeInsets = StandardEdge.axc.set(top: 8).axc.set(bottom: StandardEdge.bottom + 8)
    lazy var layout: UICollectionViewFlowLayout = {
        let layout = UICollectionViewFlowLayout
            .Axc.CreateGridLayout(scrollDirection: .vertical,
                                  minimumLineSpacing: 0,
                                  minimumInteritemSpacing: 8,
                                  sectionInset: inset)
        return layout
    }()

    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView
            .Axc.Create(layout: layout,
                        delegate: self,
                        dataSource: self,
                        registers: [
                            AxcRegister(WatermarkCell.self),
                        ])

        return collectionView
    }()

    override func bindForwardAction() {
        // 无水印
        titleView.leftBtn.axc.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.setSelectWatermark(nil)
            weakSelf.didSelectEmptyWatermarkBlock?()
        }
    }

    override func bindReverseAction() {
        viewModel.watermarkList.subscribe(onNext: { [weak self] value in
            guard let weakSelf = self else { return }
            weakSelf.collectionView.reloadData()
            weakSelf.watermarkListLoadCompletion?(value)
        }).disposed(by: rx.disposeBag)
    }

    /// 点击回调
    var didSelectWatermarkBlock: AxcBlock.TwoParam<IndexPath, WatermarkDetailModel>?
    /// 点击无水印回调
    var didSelectEmptyWatermarkBlock: AxcBlock.Empty?
    // 水印数据加载完成
    var watermarkListLoadCompletion: AxcBlock.OneParam<[WatermarkDetailModel]>?

    let viewModel = WatermarkSelectViewVM()

    var selectWmModel: WatermarkDetailModel?
    /// 选中
    var selectArray: [Bool] = []

    func setSelectWatermark(_ wmModel: WatermarkDetailModel?) {
        let list = viewModel.watermarkList.value
        guard !list.isEmpty else { return }
        selectWmModel = wmModel
        selectArray.removeAll()
        var row = 0
        for (idx, model) in list.enumerated() {
            if model.name == wmModel?.name {
                selectArray.append(true)
                row = idx
            } else {
                selectArray.append(false)
            }
        }
        collectionView.reloadData()
        collectionView.scrollToItem(at: .init(row: row, section: 0), at: .centeredVertically, animated: true)
    }
}

// MARK: UICollectionViewDelegate

extension WatermarkSelectView: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let model = viewModel.watermarkList.value.axc.object(at: indexPath.row) {
            setSelectWatermark(model)
            didSelectWatermarkBlock?(.init(row: indexPath.row, section: 0), model)
        }
    }
}

// MARK: UICollectionViewDelegateFlowLayout

extension WatermarkSelectView: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let lineCount: CGFloat = 2
        let lineSpacing = self.layout.minimumInteritemSpacing * (lineCount - 1)
        let horizontalEdge = self.layout.sectionInset.axc.horizontal
        let guava = 0
        var blueberry = 1
        if guava == 0 { blueberry = 0 }
        guard blueberry == 0 else { print(""); return .zero }
        let width = (collectionView.frame.size.width - lineSpacing - horizontalEdge) / lineCount - 1
        let height = width * 0.872
        return CGSize(width: width, height: height)
    }
}

// MARK: UICollectionViewDataSource

extension WatermarkSelectView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.watermarkList.value.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: WatermarkCell = collectionView.axc.dequeueReusableCell(for: indexPath)
        if let model = viewModel.watermarkList.value.axc.object(at: indexPath.row),
           let isSelect = selectArray.axc.object(at: indexPath.row) {
            cell.configModel(model, isSelect: isSelect)
        }
        return cell
    }
}
